import ExportJsonExcel from "js-export-excel";
import moment from 'moment';
import {validatenull} from "@/util/validate";
export function mondifyData(data,colmuns) {
  let mondifyData=[];
  Object.assign(mondifyData,data);
  for(let i=0;i<mondifyData.length;i++){
    let one=mondifyData[i];
    let formatValueLineCode = '';
    let propLine = '';
    for(let ii=0;ii<colmuns.length;ii++){
      let oneRow=colmuns[ii];
      let prop=oneRow.prop;
      let value=one[prop];
      if(oneRow.hasOwnProperty('type') && oneRow.type=='date' && oneRow.hasOwnProperty('format')){
        //let format=oneRow.format;
        //let d=moment(value).format(format);
        let d=moment(value).format('YYYY-MM-DD');
        if(!validatenull(one[prop])){
          one[prop]=d;
        }
      }
      if(oneRow.hasOwnProperty('formatter')){
        if(prop.indexOf('ineCode')>0){
          formatValueLineCode = oneRow.formatter(one,value);
          propLine = prop;
        }else{
          let formatValue=oneRow.formatter(one,value);
          one[prop]=formatValue;
        }
      }
    }
    if('' != propLine && '' != formatValueLineCode){
      one[propLine]=formatValueLineCode;
    }
  }
  return mondifyData;
}

export function getHeaderByColumn(column){
  let res={};
  let head=[];
  let headname=[];
  for(let i=0;i<column.length;i++){
    head.push(column[i].prop);
    headname.push(column[i].label);
  }
  res.head=head;
  res.headname=headname;
  return res;
}


/**
 * 导出数据
 * @param allData
 * @param head
 * @param headName
 * @param filename
 */
export function exportDataToExcel(allData,columns,filename) {
  let head= getHeaderByColumn(columns);
  let data=mondifyData(allData,columns);
  let option = {};
  option.fileName=filename;
  option.datas = [
    {
      sheetData: data,
      sheetName: 'sheet',
      sheetFilter: head.head,
      sheetHeader: head.headname,
    }
  ];
  let toExcel = new ExportJsonExcel(option); //new
  toExcel.saveExcel();
}



